<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ESP32 工具调试页面</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="bg-gray-900 text-white min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <header class="text-center mb-10">
            <h1 class="text-3xl font-bold text-indigo-400 mb-2">
                <i class="fas fa-bug mr-2"></i>ESP32 开发工具调试
            </h1>
            <p class="text-gray-400">用于诊断模块加载和资源访问问题</p>
        </header>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <!-- 资源检查 -->
            <div class="bg-gray-800 rounded-lg shadow-lg p-6 border border-gray-700">
                <div class="flex items-center mb-4">
                    <i class="fas fa-file-check text-green-500 text-xl mr-3"></i>
                    <h2 class="text-xl font-semibold">资源检查</h2>
                </div>
                <div class="space-y-3">
                    <div class="flex items-center justify-between">
                        <span>state.js</span>
                        <span id="state-js-status" class="px-2 py-1 rounded text-xs">检查中...</span>
                    </div>
                    <div class="flex items-center justify-between">
                        <span>ui-manager.js</span>
                        <span id="ui-manager-js-status" class="px-2 py-1 rounded text-xs">检查中...</span>
                    </div>
                    <div class="flex items-center justify-between">
                        <span>code-generator.js</span>
                        <span id="code-generator-js-status" class="px-2 py-1 rounded text-xs">检查中...</span>
                    </div>
                    <div class="flex items-center justify-between">
                        <span>file-exporter.js</span>
                        <span id="file-exporter-js-status" class="px-2 py-1 rounded text-xs">检查中...</span>
                    </div>
                    <div class="flex items-center justify-between">
                        <span>serial-manager.js</span>
                        <span id="serial-manager-js-status" class="px-2 py-1 rounded text-xs">检查中...</span>
                    </div>
                </div>
            </div>

            <!-- 模块加载测试 -->
            <div class="bg-gray-800 rounded-lg shadow-lg p-6 border border-gray-700">
                <div class="flex items-center mb-4">
                    <i class="fas fa-download text-blue-500 text-xl mr-3"></i>
                    <h2 class="text-xl font-semibold">模块加载测试</h2>
                </div>
                <div class="space-y-4">
                    <div>
                        <button id="test-modules" class="w-full bg-indigo-600 hover:bg-indigo-700 text-white py-2 px-4 rounded-lg transition">
                            <i class="fas fa-play mr-2"></i>运行测试
                        </button>
                    </div>
                    <div id="test-result" class="text-sm p-3 rounded bg-gray-700 min-h-[100px]">
                        <p class="text-gray-400">点击上方按钮开始测试...</p>
                    </div>
                </div>
            </div>

            <!-- 路径诊断 -->
            <div class="bg-gray-800 rounded-lg shadow-lg p-6 border border-gray-700 md:col-span-2">
                <div class="flex items-center mb-4">
                    <i class="fas fa-route text-yellow-500 text-xl mr-3"></i>
                    <h2 class="text-xl font-semibold">路径诊断</h2>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <h3 class="font-medium mb-2">当前页面URL:</h3>
                        <div id="current-url" class="text-sm bg-gray-700 p-2 rounded break-all">
                            检测中...
                        </div>
                    </div>
                    <div>
                        <h3 class="font-medium mb-2">模块路径:</h3>
                        <div id="module-paths" class="text-sm bg-gray-700 p-2 rounded break-all">
                            检测中...
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="mt-8 text-center">
            <button onclick="location.reload()" class="bg-gray-700 hover:bg-gray-600 text-white font-medium py-2 px-6 rounded-lg transition">
                <i class="fas fa-sync mr-2"></i>刷新页面
            </button>
        </div>
    </div>

    <script>
        // 显示当前页面URL
        document.getElementById('current-url').textContent = window.location.href;
        
        // 显示模块路径
        const baseUrl = window.location.origin + window.location.pathname.replace('debug.html', '');
        document.getElementById('module-paths').innerHTML = `
            <div>state.js: ${baseUrl}utils/state.js</div>
            <div>ui-manager.js: ${baseUrl}utils/ui-manager.js</div>
            <div>code-generator.js: ${baseUrl}utils/code-generator.js</div>
            <div>file-exporter.js: ${baseUrl}utils/file-exporter.js</div>
            <div>serial-manager.js: ${baseUrl}utils/serial-manager.js</div>
        `;
        
        // 检查资源是否存在
        async function checkResource(url, elementId) {
            try {
                const response = await fetch(url);
                const statusElement = document.getElementById(elementId);
                if (response.ok) {
                    statusElement.textContent = '✓ 可访问';
                    statusElement.className = 'px-2 py-1 rounded text-xs bg-green-500 text-white';
                } else {
                    statusElement.textContent = `✗ ${response.status}`;
                    statusElement.className = 'px-2 py-1 rounded text-xs bg-red-500 text-white';
                }
            } catch (error) {
                const statusElement = document.getElementById(elementId);
                statusElement.textContent = '✗ 无法访问';
                statusElement.className = 'px-2 py-1 rounded text-xs bg-red-500 text-white';
            }
        }
        
        // 检查所有资源
        checkResource('./utils/state.js', 'state-js-status');
        checkResource('./utils/ui-manager.js', 'ui-manager-js-status');
        checkResource('./utils/code-generator.js', 'code-generator-js-status');
        checkResource('./utils/file-exporter.js', 'file-exporter-js-status');
        checkResource('./utils/serial-manager.js', 'serial-manager-js-status');
        
        // 模块加载测试
        document.getElementById('test-modules').addEventListener('click', async function() {
            const resultDiv = document.getElementById('test-result');
            resultDiv.innerHTML = '<p class="text-yellow-400">正在测试模块加载...</p>';
            
            try {
                // 测试模块导入
                const startTime = Date.now();
                const stateModule = await import('./utils/state.js');
                const uiModule = await import('./utils/ui-manager.js');
                const codeModule = await import('./utils/code-generator.js');
                const fileModule = await import('./utils/file-exporter.js');
                const serialModule = await import('./utils/serial-manager.js');
                const endTime = Date.now();
                
                resultDiv.innerHTML = `
                    <p class="text-green-500 mb-2">✓ 所有模块加载成功！耗时: ${endTime - startTime}ms</p>
                    <div class="text-sm space-y-1">
                        <p>• state.js: ✓</p>
                        <p>• ui-manager.js: ✓</p>
                        <p>• code-generator.js: ✓</p>
                        <p>• file-exporter.js: ✓</p>
                        <p>• serial-manager.js: ✓</p>
                    </div>
                `;
            } catch (error) {
                resultDiv.innerHTML = `
                    <p class="text-red-500 mb-2">✗ 模块加载失败</p>
                    <div class="text-sm">
                        <p><strong>错误信息:</strong></p>
                        <p class="break-all text-red-400">${error.message}</p>
                        <p class="mt-2"><strong>错误堆栈:</strong></p>
                        <pre class="text-xs bg-gray-900 p-2 rounded mt-1 overflow-auto max-h-32">${error.stack || '无堆栈信息'}</pre>
                    </div>
                `;
                console.error('模块加载错误:', error);
            }
        });
    </script>
</body>
</html>